import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
// 加载中动画
import { createVNode, render } from 'vue';
import LoadingBar from '@/views/routerTest/LoadingBar.vue';
// 导入组件
function importBar() {
  const barLoading = createVNode(LoadingBar);
  render(barLoading, document.body);
  return barLoading;
}

declare module 'vue-router' {
  interface RouteMeta {
    title: string;
    transition: string;
  }
}

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'a',
    component: () => import('@/views/routerTest/A.vue'),
    meta: {
      title: 'A',
      transition: 'animate__backInLeft'
    }
  },
  {
    path: '/B',
    name: 'b',
    component: () => import('../views/routerTest/B.vue'),
    meta: {
      title: 'B',
      transition: 'animate__swing'
    }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

// 全局前置守卫
router.beforeEach((to, form, next) => {
  // console.log('全局前置守卫');
  // 开启加载条
  const barLoading = importBar();
  barLoading.component?.exposed?.startLoading();
  next();
});

// 全局后置守卫
router.afterEach((to, from) => {
  // 结束动画
  const barLoading = importBar();
  barLoading.component?.exposed?.endLoading();
});
export default router;
